/**
 * 
 */
package org.rvk.vms.web.controller.secure;

import org.rvk.vms.dal.entities.User;
import org.rvk.vms.service.user.VmsUser;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * Secure Home controller
 * 
 * @author Ashish Kumar Sinha <aksinha.mail@gmail.com>
 *
 */
@Controller
@RequestMapping(value="/secure/home")
public class HomeController {
	
	@RequestMapping(value="/dashboard")
	public ModelAndView dashboard(HttpServletRequest request, Principal principal){
		User user = null;
		if(principal != null) {
			UsernamePasswordAuthenticationToken authToken = (UsernamePasswordAuthenticationToken) principal;
			VmsUser vmsUserPrincipal = (VmsUser) authToken.getPrincipal();
			if(vmsUserPrincipal != null) {
				user = vmsUserPrincipal.getUser();
			}else{
				System.out.println("Principal under Auth Token is NULL.");
			}
		}else{
			System.out.println("Principal object in NULL.");
		}
		request.setAttribute("page", "HOME");
		request.setAttribute("subpage", "DASHBOARD");
		
		ModelAndView modelAndView = new ModelAndView("secure/home/dashboard", "user", user) ;
		return modelAndView;
	}
	
	@RequestMapping(value="/activities")
	public ModelAndView activities(HttpServletRequest request){
		request.setAttribute("page", "HOME");
		request.setAttribute("subpage", "ACTIVITIES");
		
		ModelAndView modelAndView = new ModelAndView("secure/home/activities") ;
		return modelAndView;
	}
}
